﻿using System;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DeepZoomPivotConstructor.VisualLib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RenderEngineInterfaces;
using SimpleFileRendering;
using t_TestingLibrary;

namespace t_SimpleFileRendering
{
    [TestClass]
    public class t_GIFFileRender
    {
        [TestInitialize]
        public void InitForTest()
        {
            t_TestingLibrary.Utilities.MyClassInit();
        }

        [TestCleanup]
        public void ShutDownTest()
        {
            t_TestingLibrary.Utilities.MyClassDone();
        }

        [TestMethod]
        public void TestNonExistantFile()
        {
            GIFFileRender r = new GIFFileRender();
            Assert.AreEqual(RenderPluginPriority.RPPCantDoIt, r.RenderPriority(new FileInfo("stupiddude.ps")));
            Assert.AreEqual(RenderPluginPriority.RPPCantDoIt, r.RenderPriority(new FileInfo("stupiddude.jpeg")));
        }

        [TestMethod]
        public void TestExistingFileWrongType()
        {
            GIFFileRender r = new GIFFileRender();
            FileInfo f = new FileInfo(@"t_JPEGFileRender.cs");
            using (var output = f.CreateText())
            {
                output.WriteLine("right");
                output.Close();
            }
            Assert.IsTrue(f.Exists, "File to test on does not exist");
            Assert.AreEqual(RenderPluginPriority.RPPCantDoIt, r.RenderPriority(f));
        }

        [TestMethod]
        [DeploymentItem("basic.gif")]
        public void TestExistingFile()
        {
            GIFFileRender r = new GIFFileRender();
            FileInfo f = new FileInfo(@"basic.gif");
            Assert.IsTrue(f.Exists, "File to test on does not exist");
            Assert.AreEqual(RenderPluginPriority.RPPGeneric, r.RenderPriority(f));
        }

        [Export(typeof(IRenderEngine))]
        public class MyCheapRender : IRenderEngine
        {
            public Task<Visual> RenderToFuture(object what, Type[] disallowRenderPlugins = null, bool forceAsComposableUnit = false, bool asComposableUnitIfNoneWithin = false)
            {
                throw new NotImplementedException();
            }

            /// <summary>
            /// Here we render a png file. Barf if it isn't a png file!
            /// </summary>
            /// <param name="what"></param>
            /// <param name="disallowRenderPlugins"></param>
            /// <param name="forceAsComposableUnit"></param>
            /// <returns></returns>
            public Visual Render(object what, Type[] disallowRenderPlugins = null, bool forceAsComposableUnit = false, bool asComposableUnitIfNoneWithin = false)
            {
                FileInfo f = what as FileInfo;
                if (f == null)
                    return null;

                if (f.Extension != ".png")
                    return null;

                Trace.WriteLine("Rendering file " + f.FullName);

                return new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            }
        }

        [TestMethod]
        [DeploymentItem("basic.gif")]
        public void TestBasicRender()
        {
            GIFFileRender r = new GIFFileRender();
            FileInfo f = new FileInfo(@"basic.gif");
            Utilities.AddPart(new MyCheapRender());
            Utilities.Compose(r);

            var result = r.Render(f);

            Assert.IsNotNull(result, "Expected non-null task to come back!");
            var rm = result.Result;
            Assert.IsNotNull(rm, "Expected the task to return somethign useful too!");
            Assert.AreEqual(1, rm.SGNodeList.Count(), "Incorrect # of images came back!");
        }

        [Export("RenderingTempDir")]
        DirectoryInfo TempDir;

        [TestMethod]
        [DeploymentItem("basic.gif")]
        public void TestTempDirectory()
        {
            DirectoryInfo info = new DirectoryInfo(".\\TestTempDirectory");
            if (info.Exists)
            {
                info.Delete(true);
            }
            info.Create();
            TempDir = info;

            GIFFileRender r = new GIFFileRender();
            FileInfo f = new FileInfo(@"basic.gif");
            Utilities.AddPart(new MyCheapRender());
            Utilities.AddPart(this);
            Utilities.Compose(r);

            var result = r.Render(f);

            var rm = result.Result;

            Assert.AreEqual(1, info.EnumerateFiles().Count(), "Expected only a single temp file!");
        }

#if false
        // On windows 8 this no longer fails! Or .NET 4.5 I guess??
        [TestMethod]
        [DeploymentItem("OutOfMemroryGIF.gif")]
        public void TestCorruptGIFFile()
        {
            GIFFileRender r = new GIFFileRender();
            FileInfo f = new FileInfo(@"OutOfMemroryGIF.gif");
            Utilities.AddPart(new MyCheapRender());
            Utilities.Compose(r);

            var result = r.Render(f);
            Assert.IsNotNull(result, "Expected a rendering!");
            Assert.IsNull(result.Result, "expected null for a corrupt GIF file!");
        }
#endif

    }
}
